home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2108 / 2108.xpi / content / domiOverlay.js < prev    next >
Text File  |  2009-04-01  |  4KB  |  127 lines

  1. var stylishDomi = {
  2.     generateSelectors: function(event) {
  3.         var node = viewer.selectedNode;
  4.         if (!(node instanceof Element)) {
  5.             return;
  6.         }
  7.         var popup = event.target;
  8.  
  9.         //element selector
  10.         stylishDomi.addSelectorMenuItem(popup, node.nodeName);
  11.         //id selector
  12.         if (node.hasAttribute("id")) {
  13.             stylishDomi.addSelectorMenuItem(popup, "#" + node.getAttribute("id"));
  14.         }
  15.         //class selector
  16.         if (node.hasAttribute("class")) {
  17.             var classes = node.getAttribute("class").split(/\s+/);
  18.             stylishDomi.addSelectorMenuItem(popup, "." + classes.join("."));
  19.         }
  20.         //attribute selectors. it's pointless to create a complicated attribute selector including an id or only a class
  21.         if (node.attributes.length > 1 || (node.attributes.length == 1 && node.attributes[0].name != "id" && node.attributes[0].name != "class")) {
  22.             var selector = node.nodeName;
  23.             for (var i = 0; i < node.attributes.length; i++) {
  24.                 if (node.attributes[i].name != "id") {
  25.                     selector += "[" + node.attributes[i].name + "=\"" + node.attributes[i].value + "\"]";
  26.                 }
  27.             }
  28.             stylishDomi.addSelectorMenuItem(popup, selector);
  29.         }
  30.         //position selector - worthless if we have an id
  31.         if (!node.hasAttribute("id") && node != node.ownerDocument.documentElement) {
  32.             stylishDomi.addSelectorMenuItem(popup, stylishDomi.getPositionalSelector(node));
  33.         }
  34.     },
  35.  
  36.     addSelectorMenuItem: function(popup, selector) {
  37.         var menuitem = document.createElementNS(stylishCommon.XULNS, "menuitem");
  38.         menuitem.setAttribute("label", selector);
  39.         menuitem.setAttribute("oncommand", "stylishDomi.copySelectorToClipboard(event)");
  40.         popup.appendChild(menuitem);
  41.     },
  42.  
  43.     getPositionalSelector: function(node) {
  44.         if (node instanceof Document) {
  45.             return "";
  46.         }
  47.         if (node.hasAttribute("id")) {
  48.             return "#" + node.getAttribute("id");
  49.         }
  50.         //are we the only child of the parent with this node name?
  51.         var uniqueChild = true;
  52.         var nodeName = node.nodeName;
  53.         for (var i = 0; i < node.parentNode.childNodes.length; i++) {
  54.             var currentNode = node.parentNode.childNodes[i];
  55.             //css ignores everything but elements
  56.             if (!(currentNode instanceof Element)) {
  57.                 continue;
  58.             }
  59.             if (node != currentNode && node.nodeName == currentNode.nodeName) {
  60.                 uniqueChild = false;
  61.                 break;
  62.             }
  63.         }
  64.         if (uniqueChild) {
  65.             return stylishDomi.getParentPositionalSelector(node) + node.nodeName;
  66.         }
  67.         //are we the first child?
  68.         if (stylishDomi.isCSSFirstChild(node)) {
  69.             return stylishDomi.getParentPositionalSelector(node) + node.nodeName + ":first-child";
  70.         }
  71.         //are we the last child?
  72.         if (stylishDomi.isCSSLastChild(node)) {
  73.             return stylishDomi.getParentPositionalSelector(node) + node.nodeName + ":last-child";
  74.         }
  75.         //get our position among our siblings
  76.         var selectorWithinSiblings = ""
  77.         for (var i = 0; i < node.parentNode.childNodes.length; i++) {
  78.             var currentNode = node.parentNode.childNodes[i];
  79.             //css ignores everything but elements
  80.             if (!(currentNode instanceof Element)) {
  81.                 continue;
  82.             }
  83.             if (currentNode == node) {
  84.                 selectorWithinSiblings += node.nodeName;
  85.                 break;
  86.             }
  87.             if (stylishDomi.isCSSFirstChild(currentNode)) {
  88.                 selectorWithinSiblings += currentNode.nodeName + ":first-child + ";
  89.             } else {
  90.                 selectorWithinSiblings += currentNode.nodeName + " + ";
  91.             }
  92.         }
  93.             return stylishDomi.getParentPositionalSelector(node) + selectorWithinSiblings;
  94.     },
  95.  
  96.     isCSSFirstChild: function(node) {
  97.         for (var i = 0; i < node.parentNode.childNodes.length; i++) {
  98.             var currentNode = node.parentNode.childNodes[i];
  99.             if (currentNode instanceof Element) {
  100.                 return currentNode == node;
  101.             }
  102.         }
  103.         return false;
  104.     },
  105.  
  106.     isCSSLastChild: function(node) {
  107.         for (var i = node.parentNode.childNodes.length - 1; i >= 0 ; i--) {
  108.             var currentNode = node.parentNode.childNodes[i];
  109.             if (currentNode instanceof Element) {
  110.                 return currentNode == node;
  111.             }
  112.         }
  113.         return false;
  114.     },
  115.  
  116.     getParentPositionalSelector: function(node) {
  117.         if (node.parentNode instanceof Document) {
  118.             return "";
  119.         }
  120.         return stylishDomi.getPositionalSelector(node.parentNode) + " > ";
  121.     },
  122.  
  123.     copySelectorToClipboard: function(event) {
  124.         Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper).copyString(event.target.getAttribute("label"));
  125.     }
  126. }
  127.